package com.liveinstars.im.endpoint;

import com.liveinstars.im.core.service.UserService;
import com.liveinstars.api.im.ChatUserApi;
import com.liveinstars.api.im.vo.ImUserVO;
import com.liveinstars.im.common.exception.IMRuntimeException;
import com.onepiece.shipelves.common.Result;
import com.onepiece.shipelves.common.enums.SystemExceptionEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: zhouze
 * @date: 2020/5/12
 */
@RestController
public class ChatUserEndpoint implements ChatUserApi {

    @Autowired
    private UserService userService;

    @Override
    public Result shieldUser(@RequestHeader("uid") Long userId,
                                      @RequestParam("shield_user_id")Long shieldUserId,
                                      @RequestParam("shield")Boolean shield) {
        Result result = new Result();
        try {
            userService.shieldUser(userId, shieldUserId, shield);
        } catch (Exception e) {
            if (e instanceof IMRuntimeException) {
                result = ((IMRuntimeException) e).createResult();
            } else {
                result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
            }
        }
        return result;
    }

    @Override
    public Result<ImUserVO> getImUserInfo(@RequestHeader("uid")Long selfId, @RequestParam("user_id")Long userId) {
        Result<ImUserVO> result = new Result<>();
        try {
            ImUserVO userInfo = userService.getImUserInfo(selfId, userId);
            result.setData(userInfo);
        } catch (Exception e) {
            if (e instanceof IMRuntimeException) {
                result = ((IMRuntimeException) e).createResult();
            } else {
                result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
            }
        }
        return result;
    }

    @Override
    public Result<Boolean> isShield(@RequestHeader("uid")Long selfId, @RequestParam("user_id")Long userId) {
        Result<Boolean> result = new Result<>();
        try {
            Boolean shield = userService.isShield(selfId, userId);
            result.setData(shield);
        } catch (Exception e) {
            if (e instanceof IMRuntimeException) {
                result = ((IMRuntimeException) e).createResult();
            } else {
                result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
            }
        }
        return result;

    }
}
